﻿var imageVisitsVM = {
    ImageVisits: ko.observableArray([]),
    ImageVisitsPage: ko.observable(0),
    ImageVisitsPageCount: ko.observable(0),
    ImageVisitsPageNavHandling: function (indexController) {
        var self = this;
        if (self.ImageVisitsPage() > 1) {
            indexController.enableImageVisitPreviousGroup(true);
        }
        if (self.ImageVisitsPage() < self.ImageVisitsPageCount()) {

            indexController.enableImageVisitNextGroup(true);
        }
    },
    sortImageVisitsDESC: function () {
        this.ImageVisits.sort(function (left, right) {
            if (left.ImageVisitID.length < right.ImageVisitID.length) {
                //alert(left.ImageVisitID.length);
                return 1;
            }   
            else if (left.ImageVisitID.length > right.ImageVisitID.length) {
                return -1;
            }   
            else {
                return left.ImageVisitID == right.ImageVisitID ? 0 : (left.ImageVisitID < right.ImageVisitID ? 1 : -1);
            }
        });
    },
    loadImageVisitsPageRe: null,
    cancelLoadImageVisitsPageRe: function () {
        var self = this;
        if (self.loadImageVisitsPageRe != null) {
            self.loadImageVisitsPageRe.abort();
        }
        //messageController.toggleMessage("cancel_request");
    },
    loadImageVisitsPage: function (id, indexController, pageNumber) {
        var self = this;
        //clear old data
        self.ImageVisits.removeAll();
        indexController.message.processing("loading");
        //stop handling
        indexController.enableListImageVisit(false);
        indexController.enableImageVisitPreviousGroup(false);
        indexController.enableImageVisitNextGroup(false);
        self.loadImageVisitsPageRe = $.ajax({
            type: 'post',
            url: home + 'Radiologist/ListOfImageVisit',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ PatientID: id, RowsPerPage: 10, PageNumber: pageNumber }),
            success: function (data) {
                self.ImageVisits(data[0]);
                //self.sortImageVisitsDESC();
                self.ImageVisitsPage(data[1].PageNumber);
                self.ImageVisitsPageCount(data[1].PageCount);
                self.ImageVisitsPageNavHandling(indexController);
                indexController.enableListImageVisit(true);
                messageController.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            }
        });
    }
};
var ImageVisitsPageInfo = ko.computed(function () {
    return "Page: " + imageVisitsVM.ImageVisitsPage() + "/" + imageVisitsVM.ImageVisitsPageCount();
});